package com.csmarket.csmarketbackend.service;

import com.csmarket.csmarketbackend.mapper.UserMapper;
import com.csmarket.csmarketbackend.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public Users getUserByName(String username) {
        return userMapper.getUserByName(username);
    }

    public boolean authenticate(String username, String password) {
        Users user = userMapper.getUserByName(username);
        // 实际项目中应该使用密码加密，这里简化处理
        return user != null && user.getPassword().equals(password);// 判断用户名和密码是否匹配
    }
    public boolean registerUser(String username, String password) {
        // 检查用户名是否已存在
        if (userMapper.getUserByName(username) != null) {
            return false; // 用户名已存在
        }

        // 创建新用户，默认角色为"user"
        Users newUser = new Users();
        newUser.setUsername(username);
        newUser.setPassword(password); // TODO 实际项目中应加密存储
        newUser.setRole("user");

        try {
            userMapper.insertUser(newUser);
            return true;
        } catch (Exception e) {
            return false; // 注册失败
        }
    }

}
